//
//  StudyChildViewController.swift
//  BLH
//
//  Created by zhangshumeng on 2021/8/12.
//

import JXCategoryView

class StudyChildViewController: BaseViewController {

    var cateId = ""
    var secId = ""
    var imageName: String = ""
    private var imageViewHeight: CGFloat = 0
    private var dataSource = [StudySecItemModel]()
    
    private lazy var scrollView: UIScrollView = {
        let view = UIScrollView()
        view.bounces = false
        return view
    }()
    
    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFit
        return view
    }()
    
    private lazy var tableView: UITableView = {
        let view = UITableView(frame: .zero, style: .plain)
        view.separatorStyle = .none
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .clear
        view.register(cellWithClass: StudyUnitCell.self)
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addSubviews()
        requestStudyUnitData()
    }

    private func addSubviews() {
        disablesAdjustScrollViewInsets(scrollView)
        disablesAdjustScrollViewInsets(tableView)
        
        /// 设置image，并计算image高度
        if let image = UIImage(named: imageName) {
            imageView.image = image
            let imageW = image.size.width
            let imageH = image.size.height
            let sc = imageH/imageW
            imageViewHeight = UIScreen.width * sc
        }
        
        view.addSubview(scrollView)
        scrollView.addSubview(imageView)
        scrollView.addSubview(tableView)
        
        scrollView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        imageView.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.width.equalTo(UIScreen.width)
            make.height.equalTo(imageViewHeight)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview()
            make.width.equalTo(UIScreen.width)
            make.height.equalTo(imageViewHeight - 70)
        }
        
        scrollView.contentSize = CGSize(width: UIScreen.width, height: imageViewHeight)
        scrollView.scrollToBottom()
    }

}

extension StudyChildViewController {
    
    private func requestStudyUnitData() {
        Network.School
            .studyUnitList(cateId: cateId, secId: secId)
            .request()
            .responseData(StudyUnitModel.self) { [weak self] model in
                guard let `self` = self else { return }
                self.dataSource = model.result.items.sorted(by: {
                    $0.index > $1.index
                })
                self.tableView.reloadData()
        } failure: { error in
            Toast.show(info: error.errorMessage)
        }
    }
}

extension StudyChildViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: StudyUnitCell.self)
        cell.backgroundColor = .clear
        cell.iconView.kf_set(dataSource[indexPath.row].coverVer)
        cell.updateLayout(indexPath.row)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    /// 根据总高度和item的数量计算cell高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return (imageViewHeight - 70) / CGFloat(dataSource.count)
    }
}

extension StudyChildViewController: JXCategoryListContentViewDelegate {
    func listView() -> UIView! {
        return view
    }
}
